﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SpriteEditor {
    public partial class frmMain : Form
    {
        //Variables
        public ModuleSaveTask moduleTasks = new ModuleSaveTask();
        public bool isUndoRedo = false;

        //Functions
        public bool haveModifiedModule()
        {
            if (spr == null || spr.lst_Modules == null)
                return false;

            if (moduleTasks.lstSaveListModule.Count != spr.lst_Modules.Count)
                return true;

            for (int i = 0; i < spr.lst_Modules.Count; i++)
            {
                if (moduleTasks.currentUseTask >= moduleTasks.lstSaveListModule.Count)
                    return false;
                if (spr.lst_Modules[i].compareTo(moduleTasks.lstSaveListModule[moduleTasks.currentUseTask][i]) == false)
                    return true;
            }
            return false;
        }

        public void saveModuleTask()
        {
            if (moduleTasks.currentUseTask <= SAVE_TASK_NUM - 1)
            {
                if (moduleTasks.lstSaveListModule.Count == SAVE_TASK_NUM)
                {
                    moduleTasks.lstCurrentSeclectionModule.RemoveAt(0);
                    moduleTasks.lstSaveListModule.RemoveAt(0);
                }
                if (moduleTasks.currentUseTask < SAVE_TASK_NUM - 1)
                    ++moduleTasks.currentUseTask;
                ModuleSelection ms = new ModuleSelection();
                if (CurSel_Module != null)
                {
                    ms = CurSel_Module.clone();

                }
                moduleTasks.lstCurrentSeclectionModule.Add(ms);

                if (spr != null && spr.lst_Modules != null)
                {
                    List<Module> listModule = new List<Module>();

                    foreach (Module mitem in spr.lst_Modules)
                    {
                        Module m = new Module();
                        m = mitem.clone();
                        listModule.Add(m);
                    }

                    moduleTasks.lstSaveListModule.Add(listModule);
                }
                int num = moduleTasks.lstSaveListModule.Count - (moduleTasks.currentUseTask + 1);
                if (num > 0)
                {
                    moduleTasks.lstSaveListModule.RemoveRange(moduleTasks.currentUseTask + 1, num);
                    moduleTasks.lstCurrentSeclectionModule.RemoveRange(moduleTasks.currentUseTask + 1, num);
                }
            }
        }

        public void undoModuleTask()
        {
            isUndoRedo = true;
            if (moduleTasks.lstSaveListModule != null)
            {
                if (moduleTasks.currentUseTask > 0)
                {
                    --moduleTasks.currentUseTask;
                }
                //spr.lst_Modules.Clear();
                spr.lst_Modules = moduleTasks.lstSaveListModule[moduleTasks.currentUseTask];
                refreshModuleGrid();
                DrawPanelModule();
                CurSel_Module = moduleTasks.lstCurrentSeclectionModule[moduleTasks.currentUseTask];
                updateModulesGridFromCurSel_Module();
            }
            isUndoRedo = false;
        }

        public void redoModuleTask()
        {
            isUndoRedo = true;
            if (moduleTasks.lstSaveListModule != null)
            {
                if (moduleTasks.currentUseTask < moduleTasks.lstSaveListModule.Count - 1)
                {
                    ++moduleTasks.currentUseTask;
                }
                spr.lst_Modules = moduleTasks.lstSaveListModule[moduleTasks.currentUseTask];
                refreshModuleGrid();
                DrawPanelModule();
                CurSel_Module = moduleTasks.lstCurrentSeclectionModule[moduleTasks.currentUseTask];
                updateModulesGridFromCurSel_Module();
            }
            isUndoRedo = false;
        }
    }
}
